Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
Listing 23.8 Command Button Click event procedure for the Invoices form.
    Private Sub Command1_Click(Index As Integer)

    Select Case Index
        Case 0      ‘ Save invoice button.
            ‘ Hide the grid then hide the form.
            DataGridItems.Visible = False
            InvoiceNumber = 0
            Hide
        Case 1      ‘ Add wine to invoice button.
            If txtCustPO.Text = “” Then
                MsgBox (“Customer PO required.”)
                txtCustPO.SetFocus
                Exit Sub
            End If
            ‘ Create a new Item.
            AdodcItems.Recordset.AddNew
            ‘ If a new invoice has not been created, then
            ‘ create a new record in the Invoices table.
            If InvoiceNumber = 0 Then CreateNewInvoice (0)
            ‘ Put the invoice number in the text box that
            ‘ is bound to that field in the Items table.
            txtInvNumber.Text = InvoiceNumber
            ‘ Call AddWine function, which returns True if the user
            ‘ selects a wine, False if they Cancel.
            If AddWine() Then   ‘ If a wine was selected
                ‘ Update the table and refresh it to include
                ‘ only items that are part of the current invoice.
                AdodcItems.Recordset.Update
                AdodcItems.RecordSource = _
                    “Select * from Items where InvNo = ” _
                    & txtInvNumber.Text
                AdodcItems.Refresh
                ‘ Make the DataGrid visible and refresh it.
                DataGridItems.Visible = True
                DataGridItems.Refresh
              Else  ‘ If the user canceled entry of a wine,
                ‘ cancel the new record. It is necessary
                ‘ to temporarily disconnect the DataGrid
                ‘ from the ADO Data control to prevent an error.
                Set DataGridItems.DataSource = Nothing
                AdodcItems.Recordset.CancelUpdate
                Set DataGridItems.DataSource = AdodcItems
            End
        Case 2      ‘ Cancel new invoice.
            ‘ If the user cancels the entire invoice.
            If InvoiceNumber <> 0 Then
                InvoiceNumber = 0
                ‘ Delete new records from the Items table.
                AdodcItems.Recordset.Filter = adFilterFetchedRecords
                AdodcItems.Recordset.Delete adAffectGroup
                DataGridItems.Visible = False
                ‘ Delete new record from the Invoices table.
                CreateNewInvoice (InvoiceNumber)
            End If
            Hide
    End Select

    End Sub
The final procedure that you need is the AddWine function, which links code in the Invoices form to the Select Wine form. This function, shown in Listing 23.9, is called by code in the Command1_Click event procedure when the user clicks the Add Wine button. It displays the Select Wine form by executing its Show method. The form is displayed modally by passing the argument 1 to the Show method, which means that the user cannot interact with any other forms until this